<template>
  <div style="width: 100%; height: 100%">
    <div class="login">
      <!--  顶部标题  -->
      <!-- <div class="el-login-header">
        <img
          src="../assets/images/login/name.png"
          style="height: 83px; width: 203px"
        />
      </div> -->
      <!--上面的部分-->
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <div class="login_left" @click="goBackLogin">
          <div class="left-title">
            <p class="title"></p>
            <p class="title">后台管理系统</p>
          </div>
        </div>
        <div class="login_right" v-if="page == 0">
          <div class="right-title-content">
            <div class="right-title" @click="changeKind(1)" id="account">
              账号登录
            </div>
            <div class="right-title-auto" @click="changeKind(2)" id="phone">
              手机号登录
            </div>
          </div>
          <!--登录状态切换-->
          <div class="right-content">
            <div v-if="kind == 1">
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  auto-complete="off"
                  placeholder="用户名"
                  class="username"
                ></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  auto-complete="off"
                  placeholder="密码"
                  class="password"
                  @keyup.enter.native="handleLogin"
                  show-password
                ></el-input>
              </el-form-item>
              <el-form-item prop="code" v-if="captchaOnOff">
                <el-input
                  v-model="loginForm.code"
                  auto-complete="off"
                  placeholder="验证码"
                  style="width: 63%"
                  class="code"
                  @keyup.enter.native="handleLogin"
                ></el-input>

                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div>
              </el-form-item>
            </div>
            <div v-else>
              <el-form-item prop="phoneNo">
                <el-input
                  v-model="loginForm.phoneNo"
                  type="text"
                  auto-complete="off"
                  placeholder="手机号"
                  class="username"
                ></el-input>
              </el-form-item>
              <el-form-item prop="phonecode">
                <el-input
                  v-model="loginForm.phonecode"
                  auto-complete="off"
                  placeholder="验证码"
                  style="width: 63%"
                  class="code"
                  @keyup.enter.native="getPhoneCode"
                ></el-input>
                <el-button
                  v-show="show"
                  size="large"
                  style="width: 34%; margin-left: 3%"
                  class="getCode"
                  type="primary"
                  @click="getPhoneCode"
                  >获取验证码</el-button
                >
                <el-button
                  v-show="!show"
                  size="large"
                  @click="getPhoneCode"
                  style="width: 34%; margin-left: 3%"
                  type="primary"
                  >{{ count }} s</el-button
                >
              </el-form-item>
            </div>

            <!--登录-->
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="
                  width: 100%;
                  height: 40px;
                  border-radius: 1px;
                  background-color: #e60012;
                "
                @click="handleLogin"
              >
                <span v-if="!loading" style="color: #fff">登 录</span>
                <span v-else style="color: #fff">登 录 中...</span>
              </el-button>
            </el-form-item>
            <!--记住密码-->
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              >记住密码</el-checkbox
            >
            <el-button type="text" style="margin-left: 150px" @click="forgetPwd"
              >忘记密码</el-button
            >
          </div>
        </div>
        <Password v-else @goBackLogin="goBackLogin"></Password>
      </el-form>
      <!-- 技术支持部分 -->
      <!-- <div class="el-login-bottom">
        小白Rachel https://blog.csdn.net/Sabrina_cc
      </div> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg, getPhoneCode, loginPhone } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getToken, setToken, removeToken } from "@/utils/auth";
import Password from "./password";

export default {
  name: "Login",
  components: { Password },
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        phoneNo: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" }],
        phoneNo: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
          {
            type: "number",
            message: "手机号格式不正确",
            trigger: "blur",
            transform(value) {
              var phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/;
              if (!phonereg.test(value)) {
                return false;
              } else {
                return Number(value);
              }
            },
          },
        ],
        phonecode: [
          { required: true, trigger: "blur", message: "请输入验证码" },
        ],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
      page: 0, // 0-登陆页面 1-忘记密码页面
      kind: 1, // 登录形式 1-账号登录 2-手机号登录
      show: true, // 手机验证码发送时间
      count: "", //倒计时
      userKind: 0, // 是否是实施人员，是1 否0
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    // 获取手机验证码
    getPhoneCode() {
      // 获取验证码
      const TIME_COUNT = 60;
      if (this.loginForm.phoneNo !== "") {
        const phoneNo = this.loginForm.phoneNo + "";
        getPhoneCode({ phoneNo: phoneNo }).then((res) => {
          console.log(res);
          if (res.code === 200) {
            this.$message.success("验证码获取成功");
            this.count = TIME_COUNT;
            this.show = false;
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--;
              } else {
                this.show = true;
                clearInterval(this.timer);
                this.timer = null;
              }
            }, 1000);
          } else {
            this.$message.error(res.msg);
          }
          // this.resData = res.list
        });
      } else {
        Message.error("请输入正确的手机号");
        // this.$message.error(res.msg);
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
    // 忘记密码
    forgetPwd() {
      this.page = 1;
    },
    // 登录形式转化
    changeKind(e) {
      this.kind = e;
      // 每次修改清空表单校验
      // this.loginForm = {};
      this.$refs.loginForm.clearValidate();
      // 修改样式
      if (e == 2) {
        document.getElementById("phone").style.borderBottom =
          "2px solid #e60012";
        document.getElementById("account").style.borderBottom = "none";
      } else {
        document.getElementById("phone").style.borderBottom = "none";
        document.getElementById("account").style.borderBottom =
          "2px solid #e60012";
      }
    },
    // 切换到登录页面
    goBackLogin() {
      this.page = 0;
      this.kind = 1;
    },
  },
};
</script>

<style lang="scss" rel="stylesheet/scss"  scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;

  margin: 0 auto;
  width: 100%;
  height: 100%;
  /*width: 1920px;*/
  /*height: 1080px;*/
  background: url("../assets/images/login/bg.png") center no-repeat;
  background-size: 100%;
}

.login-form {
  border-radius: 6px;
  background: url("../assets/images/login/pic.png") left no-repeat;
  background-color: #fff;
  width: 800px;
  height: 500px;
  padding: 5px 25px 5px 25px;
  // margin-top: -50px;
  display: flex;

  .login_left {
    width: 300px;
    .left-title {
      margin: 60px 0px 0px 20px;
      width: 280px;
      height: 64px;
      font-size: 28px;
      font-family: Source Han Sans CN ExtraLight,
        Source Han Sans CN ExtraLight-ExtraLight;
      font-weight: ExtraLight;
      text-align: left;
      color: #ffffff;
      line-height: 40px;
      .title {
        margin: 5px;
        font-weight: 600;
      }
    }
  }
  /*右边的部分*/
  .login_right {
    width: 399px;
    margin-left: 60px;
    margin-top: 60px;
    padding: 0px 40px;
    img {
      border-radius: 4px;
      padding-bottom: 90px;
    }
    .right-title-content {
      height: 50px;
      width: 300px;
      display: flex;
      border-bottom: 1px solid #cccccc;
      .right-title {
        width: 150px;
        height: 50px;
        font-size: 24px;
        font-family: Source Han Sans CN Light, Source Han Sans CN Light-Light;
        font-weight: 400;
        text-align: center;
        color: #e60012;
        line-height: 40px;
        border-bottom: 2px solid #e60012;
      }
      .right-title-auto {
        width: 150px;
        height: 50px;
        font-size: 24px;
        font-family: Source Han Sans CN Light, Source Han Sans CN Light-Light;
        font-weight: 400;
        text-align: center;
        color: #999999;
        line-height: 40px;
        // border-bottom: 2px solid #e60012;
      }
    }
    .right-content {
      width: 300px;
      height: 350px;
      padding-top: 40px;
    }
  }
  /*输入框的间距*/
  .el-input {
    height: 42px;
    border: 1px solid #cccccc;
    // border-top: 0px;
    // border-left: 0px;
    // border-right: 0px;
  }
}
::v-deep .el-input__inner {
  font-size: 20px;
  background-color: rgba(255, 255, 255, 0);
  border: 0;
  height: 100%;
  width: 90%;
}

/*验证码*/
.login-code {
  width: 33%;
  height: 21px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  width: 105%;
  padding-bottom: 0px;
  // height:36px;
  /*height: 1px;*/
  /* background-color: #1e1e1e;*/
}

.login-form .login_right img {
  padding-bottom: 0px;
}
.el-login-header {
  height: 5px;
  position: fixed;
  /* bottom: 250px;*/
  top: 6%;
  right: 33%;
  width: 70%;
  text-align: center;
  img {
    width: 30%;
  }
}
.el-login-bottom {
  height: 20px;
  position: fixed;
  /* bottom: 250px;*/
  bottom: 8%;
  right: 0;
  left: 0;
  // width: 30%;
  text-align: center;
}

.username ::v-deep .el-input__inner {
  background: url("../assets/images/login/user.png") no-repeat left;
  background-size: 8%;
  margin-left: 10px;
  // width:200px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
.password ::v-deep .el-input__inner {
  background: url("../assets/images/login/pwd.png") no-repeat left;
  background-size: 8%;
  margin-left: 10px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
.code ::v-deep .el-input__inner {
  background: url("../assets/images/login/yzm.png") no-repeat left;
  background-size: 10%;
  margin-left: 10px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
</style>
